package demo03;

import java.util.function.IntConsumer;

public class ZeroEvenOdd {
	private int n;
	private int num=0;
	
	public ZeroEvenOdd(int n) {
		super();
		this.n = n;
	}
	public void zero(IntConsumer printNumber) throws Exception{
		for(int i=0;i<n;i++){
			synchronized (this) {
				while(num%2!=0){
					this.wait();
				}
				printNumber.accept(0);
				num++;
				this.notifyAll();
			}
		}
	}
	public void even(IntConsumer printNumber) throws Exception{
		for(int i=2;i<=n;i=i+2){
			synchronized (this) {
				while(num%2==0||num%4!=3){
					this.wait();
				}
				printNumber.accept(i);
				num++;
				this.notifyAll();
			}
		}
	}
	public void odd(IntConsumer printNumber) throws Exception{
		for(int i=1;i<=n;i=i+2){
			synchronized (this) {
				while(num%2==0||num%4!=1){
					this.wait();
				}
				printNumber.accept(i);
				num++;
				this.notifyAll();
			}
		}
	}
}
